/// 密码加密公钥响应
class PublicKeyResponse {
  final String keyId;
  final String type;
  final String purpose;
  final String publicKey;

  PublicKeyResponse({
    required this.keyId,
    required this.type,
    required this.purpose,
    required this.publicKey,
  });

  factory PublicKeyResponse.fromJson(Map<String, dynamic> json) {
    return PublicKeyResponse(
        keyId: json['keyId'],
        type: json['type'],
        purpose: json['purpose'],
        publicKey: json['publicKey']);
  }

  Map<String, dynamic> toJson() {
    return {
      'keyId': keyId,
      'type': type,
      'purpose': purpose,
      'publicKey': publicKey,
    };
  }
}

/// 认证token响应
class AuthTokenResponse {
  final String gsid;
  final String accessToken;
  final String tokenType;
  final String refreshToken;
  final int expiresIn;

  AuthTokenResponse({
    required this.gsid,
    required this.accessToken,
    required this.tokenType,
    required this.refreshToken,
    required this.expiresIn,
  });

  factory AuthTokenResponse.fromJson(Map<String, dynamic> json) {
    return AuthTokenResponse(
      gsid: json['gsid'],
      accessToken: json['accessToken'],
      tokenType: json['tokenType'],
      refreshToken: json['refreshToken'],
      expiresIn: json['expiresIn'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'gsid': gsid,
      'accessToken': accessToken,
      'tokenType': tokenType,
      'refreshToken': refreshToken,
      'expiresIn': expiresIn,
    };
  }
}

/// 获取个人信息响应
class AuthMeResponse {
  final String userId;
  final String username;
  final String? nickname;
  final String? email;
  final String? phone;
  final String? avatar;
  final List<String>? roles;
  final List<String>? permissions;

  AuthMeResponse({
    required this.userId,
    required this.username,
    this.nickname,
    this.email,
    this.phone,
    this.avatar,
    this.roles,
    this.permissions,
  });

  factory AuthMeResponse.fromJson(Map<String, dynamic> json) {
    return AuthMeResponse(
      userId: json['userId'],
      username: json['username'],
      nickname: json['nickname'] ?? json['username'],
      email: json['email'] ?? '',
      phone: json['phone'] ?? '',
      avatar: json['avatar'] ?? '',
      roles: List<String>.from(json['roles'] ?? []),
      permissions: List<String>.from(json['permissions'] ?? []),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'userId': userId,
      'username': username,
      'nickname': nickname,
      'email': email,
      'phone': phone,
      'avatar': avatar,
      'roles': roles,
      'permissions': permissions,
    };
  }
}

/// 登录成功响应
class AuthLoginResponse extends AuthTokenResponse {
  final AuthMeResponse user;

  AuthLoginResponse({
    required super.gsid,
    required super.accessToken,
    required super.tokenType,
    required super.refreshToken,
    required super.expiresIn,
    required this.user,
  });

  factory AuthLoginResponse.fromJson(Map<String, dynamic> json) {
    return AuthLoginResponse(
      gsid: json['gsid'],
      accessToken: json['accessToken'],
      tokenType: json['tokenType'],
      refreshToken: json['refreshToken'],
      expiresIn: json['expiresIn'],
      user: AuthMeResponse.fromJson(json['user']),
    );
  }

  @override
  Map<String, dynamic> toJson() {
    final data = super.toJson();
    data['user'] = user.toJson();
    return data;
  }
}
